package com.sinosoft.product;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Service;

@Service("productDisplay")
public class ProductDisplayManagerImpl implements ProductDisplayManager {

	@Autowired
	ProductDisplayDao dao;

	public ArrayList<ProductDisplay> getProduct(String sql, int pageStart,
			int pageEnd, HttpServletRequest request, MessageSource messageSource) {
		// TODO Auto-generated method stub
		// String sqlStart = "select * from (";
		// String sqlEnd =
		// " )dp where dp.rownumber between "+pageStart+" and "+pageEnd;
		// String sqlMid = sql.replaceFirst("from",
		// " ,rownum as rownumber from ");
		// String sqlAll = sqlStart+sqlMid+sqlEnd;

		StringBuffer sqlAll = new StringBuffer();
		sqlAll.append(" select * from ( ");
		sqlAll
				.append(sql
						.replaceFirst("from",
								" , row_number() over(order by productid desc) as rownumber from "));
		sqlAll.append(" )dp where dp.rownumber between " + pageStart + " and "
				+ pageEnd);

		return (ArrayList<ProductDisplay>) dao.getProduct(sqlAll.toString(),
				request, messageSource);
	}

	public int getCount(String sql) {
		// TODO Auto-generated method stub
		String sqlCount = "";
		String[] count = sql.split("from");
		for (int i = 0; i < count.length; i++) {
			sqlCount = "select count(*) from ";
			sqlCount = sqlCount + count[i];

		}

		return dao.getCount(sqlCount);
	}

	@SuppressWarnings("unchecked")
	public String getProductStr(String sql, HttpServletRequest request,
			MessageSource messageSource) {
		// TODO Auto-generated method stub
		String result = "";
		ArrayList<ProductDisplay> alist = new ArrayList();
		List productList = dao.getProduct(sql, request, messageSource);
		alist = (ArrayList<ProductDisplay>) productList;
		for (int i = 0; i < alist.size(); i++) {
			ProductDisplay product = new ProductDisplay();
			product = alist.get(i);

			result = product.getProductlevel() + "," + product.getSatelliate()
					+ "_" + product.getSonser() + "_" + product.getProductid();
		}
		return result;
	}

	public int getShopCarStatus(String str) {
		// TODO Auto-generated method stub
		//
		String[] param = str.split("_");
		String sql = " select count(*) as datacount from zywx_shop_info t where t.productlevel in '"
				+ param[1]
				+ "' and t.username in '"
				+ param[2]
				+ "' and t.productid in '" + param[0] + "' ";

		return dao.getShopCarStatus(sql);
	}

}
